#ifndef MSTHREAD_H
#define MSTHREAD_H

#include <libmslog/mstype.h>

#define maxnum_locklist 10240	//池子不够只是不会被记录，但并不影响使用
#define MSTHREADFunc ms_pvoid

typedef enum{
	THREAD_STATE_UNKNOW		=0,
	THREAD_STATE_INIT,
	THREAD_STATE_STARTING,
	THREAD_STATE_START,
	THREAD_STATE_STOPPING,	
	THREAD_STATE_STOP,	
}THREADstate;
typedef enum LOCKType{
	lockType_unknow		=0,
	lockType_mutex,			//普通互斥锁
	lockType_recursive,		//递归互斥锁
	lockType_spin,		//自旋锁
	lockType_rw,		//读写锁
	lockType_cond		//条件锁
}LOCKType;
typedef struct MSLOCKContext{
	ms_u08 name[128];
	pthread_mutexattr_t mattr;
	pthread_mutex_t mutex;		//互斥锁
	pthread_spinlock_t spinlock;	//自旋锁
	pthread_rwlock_t rwlock;	//读写锁
	pthread_cond_t cond;		//条件锁
	LOCKType lockType;
	ms_bool init;
	ms_bool debug;
	ms_byte owner_info[128];
}MSLOCKContext;
typedef struct MSTHREADContext{
#if defined(OS_ANDROID)||defined(OS_LINUX_SOC)
	pthread_t thread_id;		
#elif defined(OS_WIN32)
	unsigned long thread_id;
#endif
	ms_u32  use_cpuindex;
	MSLOCKContext mslock_ctt;
	ms_bool init;
	ms_bool debug;
	ms_u08 name[128];
	THREADstate state;
}MSTHREADContext;

//typedef void* (*MSTHREADFunc)(void*);
typedef struct MSLOCKLISTContext{	
	MSLOCKContext *pmslock_cct;
	ms_bool flag_used;
	ms_u08 name[128];
}MSLOCKLISTContext;

#ifndef MSTHREAD_C
extern ms_void 
	msthread_api_envInit(ms_void);
extern ms_s08 
	msthread_api_envDeinit(ms_void);
extern MSLOCKLISTContext *
	mslocklist_api_init(ms_void);
extern ms_void 
	mslocklist_api_show(MSLOCKLISTContext *pmslocklist_ctt);
extern ms_void 
	mslocklist_api_deinit(MSLOCKLISTContext *ppmslocklist_ctt);
extern ms_void 
	msthread_api_setName(ms_string ms_in name);
extern ms_void 
	msthread_api_setName2(const char *format,...);
extern ms_s08 
	msthread_api_create(MSTHREADContext * ms_io pmsthread_ctt,ms_string ms_io name,MSTHREADFunc ms_in msthrad_func,ms_pvoid ms_in arg);
extern ms_s08 
	msthread_api_destroy(MSTHREADContext * ms_io pmsthread_ctt);
extern ms_bool 
	msthread_api_waitStateTimeout(MSTHREADContext * ms_in pmsthread_ctt,THREADstate tstte_init,THREADstate tstte_wait,ms_u32 timeout_ms,ms_u32 freq_ms);	//100ms
extern ms_bool 
	msthread_api_waitCmdStarting(MSTHREADContext * ms_in pmsthread_ctt,ms_u32 timeout_ms,ms_u32 freq_ms);
extern ms_bool  
	msthread_api_isStop(MSTHREADContext * ms_in pmsthread_ctt);
extern ms_s08 
	mslock_api_init(MSLOCKContext * ms_io pmslock_ctt,ms_string ms_in name,LOCKType lockType);
extern ms_s08 
	mslock_api_obtain(MSLOCKContext * ms_in pmslock_ctt,ms_cstring ms_in func, ms_u32 ms_in line,ms_bool flag_rd);
extern ms_s08 
	mslock_api_release(MSLOCKContext * ms_in pmslock_ctt,ms_cstring ms_in func, ms_u32 ms_in line);
extern ms_s08 
	mslock_api_waitSignal(MSLOCKContext * ms_in pmslock_ctt,ms_cstring ms_in func,ms_u32 ms_in line);
extern ms_s08 
	mslock_api_sendSignal(MSLOCKContext * ms_in pmslock_ctt,ms_cstring ms_in func,ms_u32 ms_in line,ms_bool flag_all);
extern ms_s08 
	mslock_api_deinit(MSLOCKContext * ms_io pmslock_ctt);
extern ms_u32 
	mslock_api_num(ms_void);
extern ms_void 
	mslock_api_webInfo(ms_byte * outbuf);
extern ms_bool 
	mslock_api_waitStateTimeout(MSLOCKContext * ms_io pmslock_ctt,THREADstate * ms_in pthreadstate_g,THREADstate tstte_init,THREADstate tstte_wait,ms_u32 timeout_ms,ms_u32 freq_ms);	//100ms
#else
ms_s08 
	mslock_api_init(MSLOCKContext * ms_io pmslock_ctt,ms_string ms_in name,LOCKType lockType);
ms_s08 
	mslock_api_obtain(MSLOCKContext * ms_in pmslock_ctt,ms_cstring ms_in func, ms_u32 ms_in line,ms_bool flag_rd);
ms_s08 
	mslock_api_release(MSLOCKContext * ms_in pmslock_ctt,ms_cstring ms_in func, ms_u32 ms_in line);
ms_s08 
	mslock_api_waitSignal(MSLOCKContext * ms_in pmslock_ctt,ms_cstring ms_in func,ms_u32 ms_in line);
ms_s08 
	mslock_api_sendSignal(MSLOCKContext * ms_in pmslock_ctt,ms_cstring ms_in func,ms_u32 ms_in line,ms_bool flag_all);
ms_s08 
	mslock_api_deinit(MSLOCKContext * ms_io pmslock_ctt);
ms_bool 
	mslock_api_waitStateTimeout(MSLOCKContext * ms_io pmslock_ctt,THREADstate * ms_in pthreadstate_g,THREADstate tstte_init,THREADstate tstte_wait,ms_u32 timeout_ms,ms_u32 freq_ms);	//10
#endif

#define mslock_api_do(pmslock_ctt)		mslock_api_obtain(pmslock_ctt,__FUNCTION__,__LINE__,ms_false);
#define mslock_api_undo(pmslock_ctt)	mslock_api_release(pmslock_ctt,__FUNCTION__,__LINE__);

#define mslock_api_rd(pmslock_ctt)		mslock_api_obtain(pmslock_ctt,__FUNCTION__,__LINE__,ms_true);
#define mslock_api_wd(pmslock_ctt)		mslock_api_obtain(pmslock_ctt,__FUNCTION__,__LINE__,ms_false);

#define mslock_api_wait(pmslock_ctt)	mslock_api_waitSignal(pmslock_ctt,__FUNCTION__,__LINE__);
#define mslock_api_signal(pmslock_ctt,flag_all)	mslock_api_sendSignal(pmslock_ctt,__FUNCTION__,__LINE__,flag_all);
#endif